﻿using System;
using System.Security.Permissions;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Xml.XPath;
using Com.FastSearch.SharePoint.Interfaces;

namespace Com.FastSearch.SharePoint.WebControls.ESPSearchWebParts.UI
{
    /// <summary>
    /// Web Part writing the Search Statistics of the current search.
    /// xslt is used for rendering the statistics.
    /// </summary>
    public class ESPSearchStatistics : ESPSearchWebPart
    {
        #region Private variables
        /// <summary>
        /// <see cref="IXPathNavigable"/> (XmlDocument) containing the search statistics as xml document which is used for the xslt transormation.        
        /// </summary>
        private IXPathNavigable resultXmlDoc = null;
       
        #endregion        

        #region ESPSearchWebPart overrides
        /// <summary>
        /// Specify the default XSLT file used for the result list.
        /// </summary>
        /// <returns>string containing the xslt</returns>
        public override string GetEmbeddedXsl()
        {
            return LoadXslFromAssembly("SearchStatistics.xslt");
        }

        /// <summary>
        /// Add the Web Part custom properties which are search specific to the <see cref="Com.FastSearch.SharePoint.WebControls.ESPSearchWebParts.UI.Client.SearchRequest"/><br/>
        /// See <see cref="ESPSearchWebPart.ConfigureSearch"/> for details.
        /// Nothing is used by this webpart. Just implementing the abstract method
        /// </summary>
        protected override void ConfigureSearch()
        {
            // Nothing to do            
        }

        /// <summary>
        /// Return the xpath navigator for the result xml object used by the xslt transformation
        /// </summary>
        /// <param name="viewPath">View path for the xpath navigator. Not used as the root is returned</param>
        /// <returns><see cref="XPathNavigator"/>XPath Navigator used for the xslt rendering</returns>
        [AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
        protected override XPathNavigator GetXPathNavigator(string viewPath)
        {
            if (resultXmlDoc != null)
            {
                return resultXmlDoc.CreateNavigator();
            }
            // Empty result list
            return new XmlDocument().CreateNavigator();
        }

        /// <summary>
        /// See <see cref="ESPSearchWebPart.SetSearchResult"/>
        /// </summary>
        /// <param name="SearchResult">object containing all result information. <see cref="Com.FastSearch.SharePoint.Interfaces.IESPSearchResult"/></param>
        public override void SetSearchResult(IESPSearchResult SearchResult)
        {
            resultXmlDoc = SearchResult == null ? null : SearchResult.GetXmlSearchStatistics(SearchRequest.Start1, SearchRequest.NumberOfResultsPerPage);
        }

        #endregion
    }
}
